
use tracing_subscriber::{filter::LevelFilter, fmt::writer::MakeWriterExt};


pub fn log_init() {
    let env = std::env::var("RUST_LOG");

    let env_filter = match env {
        Ok(_) => tracing_subscriber::EnvFilter::new(
            std::env::var("RUST_LOG").unwrap_or_else(|_| "log-demo=debug,tower_http=debug".into()),
        ),
        Err(_) => tracing_subscriber::EnvFilter::builder()
            .with_default_directive(LevelFilter::DEBUG.into())
            .from_env_lossy(),
    };

    let stdout = std::io::stdout.with_max_level(tracing::Level::DEBUG);
    let file_appender =
        tracing_appender::rolling::never("/opt/soft/nacos-demo", "nacos-demo.log");
    let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
    // let fmt_layer = tracing_subscriber::fmt::layer()
    //     .with_target(false)
    //     .with_level(true) // don't include levels in formatted output
    //     .with_target(true) // don't include targets
    //     .with_thread_ids(true) // include the thread ID of the current thread
    //     .with_thread_names(true) // include the name of the current thread
    //     .with_line_number(true);

    let subscriber = tracing_subscriber::fmt()
        .with_env_filter(env_filter)
        .with_writer(stdout.and(non_blocking))
        .with_ansi(true)
        .with_max_level(tracing::Level::DEBUG)
        .with_target(false)
        .with_level(true) // don't include levels in formatted output
        .with_thread_ids(true) // include the thread ID of the current thread
        .with_thread_names(true) // include the name of the current thread
        .with_line_number(true)
        .finish();

    tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
    // tracing_subscriber::registry()
    //     .with(env_filter)
    //     .with(fmt_layer)
    //     .init()
}
